import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../gen/assets.gen.dart';

/// 健康指标节点组件
class HealthIndicatorNode extends StatelessWidget {
  /// 节点图标资源
  final AssetGenImage? iconAsset;
  
  /// 节点文字
  final String text;
  
  /// 节点颜色
  final Color color;
  
  /// 节点位置
  final Offset position;
  
  /// 节点大小
  final double size;
  
  /// 是否为主节点（影响样式）
  final bool isMainNode;
  
  /// 点击回调
  final VoidCallback? onTap;
  
  /// 子节点说明文字（仅用于子节点展示）
  final String? subtitle;

  const HealthIndicatorNode({
    super.key,
    this.iconAsset,
    required this.text,
    required this.color,
    required this.position,
    this.size = 60,
    this.isMainNode = true,
    this.onTap,
    this.subtitle,
  });

  @override
  Widget build(BuildContext context) {
    // 子节点固定尺寸为20
    final nodeSize = isMainNode ? size : 17.0;
    
    return Positioned(
      left: position.dx - (nodeSize / 2),
      top: position.dy - (nodeSize / 2),
      child: GestureDetector(
        onTap: onTap,
        child: Container(
          width: nodeSize.w,
          height: nodeSize.w,
          decoration: BoxDecoration(
            color: color.withValues(alpha: 0.3), // 彩色半透明背景
            borderRadius: BorderRadius.circular((nodeSize / 2).r),
            border: Border.all(
              color: color.withValues(alpha: 0.5),
              width: 0.5,
            ),
          ),
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                // 图标
                if (iconAsset != null) ...[
                  iconAsset!.image(
                    width: (nodeSize * 0.4).w,
                    height: (nodeSize * 0.4).w,
                    color: color,
                  ),
                  SizedBox(height: 1.h),
                ],
                // 文字 - 颜色与节点一致
                Text(
                  text,
                  style: TextStyle(
                    fontSize: isMainNode ? 8.sp : 11.sp,
                    color: color,
                    fontWeight: isMainNode ? FontWeight.w600 : FontWeight.normal,
                  ),
                  textAlign: TextAlign.center,
                ),
                if (!isMainNode && subtitle != null) ...[
                  SizedBox(height: 1.h),
                  Text(
                    subtitle!,
                    style: TextStyle(
                      fontSize: 6.sp,
                      color: const Color(0xFFB0B0B0),
                      fontWeight: FontWeight.w400,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    textAlign: TextAlign.center,
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// 健康指标节点构建器 - 用于批量创建节点
class HealthIndicatorNodeBuilder {
  /// 创建主节点
  static HealthIndicatorNode createMainNode({
    required AssetGenImage iconAsset,
    required String text,
    required Color color,
    required Offset position,
    double size = 45,
    VoidCallback? onTap,
  }) {
    return HealthIndicatorNode(
      iconAsset: iconAsset,
      text: text,
      color: color,
      position: position,
      size: size,
      isMainNode: true,
      onTap: onTap,
    );
  }

}
